package com.tutu.controller.common;

import com.tutu.controller.editor.*;
import com.tutu.utils.BaseUtil;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * 公共控制层
 * Created by 涂奕恒 on 2014/12/10.
 */
public class CommonController {

    // type == 0, get
    // type == 1, set
    public static String changeStr(String str, Integer type) {
        if (BaseUtil.isBlank(str)) return "";

        if (type == 0) {
            return str.replace("&lt", "<").replace("&gt", ">");
        } else {
            return str.replace("<", "&lt").replace(">", "&gt");
        }
    }

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(String.class, new CustomStringEditor());
        binder.registerCustomEditor(MultipartFile.class, new CustomFileEditor());
        binder.registerCustomEditor(Double.class, new CustomDoubleEditor());
        binder.registerCustomEditor(Float.class, new CustomFloatEditor());
        binder.registerCustomEditor(Integer.class, new CustomIntegerEditor());
        binder.registerCustomEditor(Long.class, new CustomLongEditor());
        binder.registerCustomEditor(Date.class, new CustomDateEditor());
    }

    public Integer getPageNum(Integer start, Integer length) {
        if (start == null) {
            start = 0;
        }
        if (length == null) {
            length = 10;
        }

        return (start / length) + 1;
    }
}